//
//  RegisterCell.m
//  youthNote
//
//  Created by huangrensheng on 16/6/13.
//  Copyright © 2016年 laber. All rights reserved.
//

#import "RegisterCell.h"

@interface RegisterCell()<UITextFieldDelegate>
@property (nonatomic, strong) UITextField *editFile;
@property (nonatomic, strong) NSString *imageName;
@property (nonatomic, strong) NSString *plach;
@property (nonatomic, strong) UIImageView *iconImage;
@end

@implementation RegisterCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if ([super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = [UIColor clearColor];
        [self initChildViews];
        [self makeConstraints];
        self.bottonLine.hidden = YES;
    }
    return self;
}

- (void)initChildViews{
    self.editFile = [self createTextFiled:self.imageName plach:self.plach];
    self.editFile.delegate = self;
    [self.contentView addSubview:self.editFile];
}

- (void)setIndexPath:(NSIndexPath *)indexPath{
    super.indexPath = indexPath;
    if (indexPath.row == 0) {
        self.editFile.placeholder = @"用户名";
        self.iconImage.image = [UIImage imageNamed:@"login_key"];
    }else if (indexPath.row == 1){
        self.editFile.secureTextEntry = YES;
        self.editFile.placeholder = @"密码";
        self.iconImage.image = [UIImage imageNamed:@"login_user"];
    }else if (indexPath.row == 2){
        self.editFile.secureTextEntry = YES;
        self.editFile.placeholder = @"确认密码";
        self.iconImage.image = [UIImage imageNamed:@"register_suer"];
    }else if (indexPath.row == 3){
        self.editFile.placeholder = @"邮箱";
        self.iconImage.image = [UIImage imageNamed:@"register_email"];
    }
}

- (UITextField *)createTextFiled:(NSString *)leftImage plach:(NSString *)plach{
    UIView *leftBack = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    self.iconImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:leftImage]];
    [leftBack addSubview:self.iconImage];
    [self.iconImage  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(leftBack.mas_centerX);
        make.centerY.equalTo(leftBack.mas_centerY);
    }];
    UITextField *textFiled = [[UITextField alloc] init];
    [textFiled.layer setCornerRadius:5];
    [textFiled.layer setBorderColor:LINE_COLOR.CGColor];
    [textFiled.layer setBorderWidth:1.0f];
    textFiled.leftView = leftBack;
    textFiled.leftViewMode = UITextFieldViewModeAlways;
    textFiled.placeholder = plach;
    return textFiled;
}

- (void)makeConstraints{
    [self.editFile mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(0, 15, -1, 15));
    }];
}

- (void)shouldEditFinish:(EditFinish)finish{
    self.editFinish = finish;
}

- (void)textFieldDidEndEditing:(UITextField *)textField{
    [self finshEditValue:textField];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self finshEditValue:textField];
    return YES;
}

- (void)finshEditValue:(UITextField *)textField{
    NSString *key = @"";
    if (self.indexPath.row == 0) {
        key = @"userName";
    }else if (self.indexPath.row == 1){
        key = @"passWord";
    }else if(self.indexPath.row == 2){
        key = @"surePassWord";
    }else if (self.indexPath.row == 3){
        key = @"email";
    }
    self.editFinish(key,textField.text);
    [textField resignFirstResponder];
}

@end
